#include "gather_v3_tiling.h"
#include "register/op_def_registry.h"
namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
  GatherV3TilingData tiling;
    auto x_shape = context->GetInputShape(0)->GetStorageShape();
    auto indices_shape = context->GetInputShape(1)->GetStorageShape();
    int32_t batch_dims = *context->GetAttrs()->GetInt(0);
    int32_t x_dim_num = x_shape.GetDimNum();
    uint16_t indices_dim_num = indices_shape.GetDimNum();
    int32_t axis = context->GetInputTensor(2)->GetData<int32_t>()[0];
    int32_t batchs = 1;
    int32_t repeat = 1;
    int32_t cycles = 1;
    int32_t interval = 1;
    for (int i = 0; i < batch_dims; i++)
        batchs *= indices_shape.GetDim(i);
    for (int i = batch_dims; i < axis; i++)
        repeat *= x_shape.GetDim(i);
    for (int i = batch_dims; i < indices_dim_num; i++)
        cycles *= indices_shape.GetDim(i);
    for (int i = axis + 1; i < x_dim_num; i++)
        interval *= x_shape.GetDim(i);
    int32_t batch_lenght = 1;
    for (int i = batch_dims; i < x_dim_num; i++)
        batch_lenght *= x_shape.GetDim(i);
    int32_t repeat_lenght = 1;
    for (int i = axis; i < x_dim_num; i++)
        repeat_lenght *= x_shape.GetDim(i);
    uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType()); 
    if(interval == 1)
    {
        context->SetTilingKey(3);
    }
    else if((interval*inputBytes)%32 == 0)
    {
        context->SetTilingKey(1);
    }
    else
    {
        context->SetTilingKey(2);
    }
    tiling.set_batchs(batchs);
    tiling.set_repeat(repeat);
    tiling.set_cycles(cycles);
    tiling.set_interval(interval);
    tiling.set_batch_lenght(batch_lenght);
    tiling.set_repeat_lenght(repeat_lenght);
  context->SetBlockDim(1);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
  return ge::GRAPH_SUCCESS;
}
}
namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
{
const auto inputDataType = context->GetInputDataType(0);
context->SetOutputDataType(0, inputDataType);
return ge::GRAPH_SUCCESS;
}
}
namespace ops {
class GatherV3 : public OpDef {
public:
    explicit GatherV3(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT8, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("indices")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("axis")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("out")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT8, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("batch_dims").AttrType(OPTIONAL).Int(0);
        this->Attr("negative_index_support").AttrType(OPTIONAL).Bool(false);
        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);
        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");
    }
};
OP_ADD(GatherV3);
}
